/*请求url*/
var urlData = require('../../../../data/url.js');

Page({
    data: {
      arr: [],
      userId:'',
      /*url*/
      courseEvaluateUrl:urlData.urlList.organManage.courseEvaluate.url,
      addValueUrl:urlData.urlList.organManage.addValue.url
    },
    /*
      页面加载
    */
    onLoad:function(e){
      var that = this;
      wx.getStorage({
        key:'loginConf',
        success:function(res){
          that.setData({
            userId:res.data.userId
          });
          that.sendUserId();
        }
      });
    },
    /*
      发送用户id，请求list
    */
    sendUserId:function(e){
      var that = this;
      var i;
      var length;
      var list;
      wx.request({
        url: this.data.courseEvaluateUrl, 
        method: "get",
        data: {
          user_id:this.data.userId
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 0) {
            length = res.data.list.length;
            list = res.data.list;
            for(i=0; i<length; i++){
              list[i].courseEva.courseId=list[i].courseEva.courseId.toString();
              list[i].teacherEva.teacherId=list[i].teacherEva.teacherId.toString();
            }
            that.setData({
              arr : list
            });
          }
        }
      });
    },
    /*
      表单提交
    */
    formSubmit: function(e) {
      var str = '',
          length = Object.keys(e.detail.value).length,
          key = Object.keys(e.detail.value),
          value = Object.values(e.detail.value),
          i,
          userId = this.data.userId;
          
      /*------------------------------------------------------------------------------------------------------
        功能：
          防止name重复发生值得覆盖
        解释：
          1.防止courseid和courseid重复：在每个传过来的courseid后面加了index，也就是0,1,2,3......
          2.防止courseid和teacherid重复：在上述处理的基础上，后面加了‘course’和‘teacher’字符
          3.最后的处理是先把‘course’或‘teacher’去掉，再把index也就是1,2,3等数字去掉
       */
      for(i=0; i<length; i+=2){
        key[i] = key[i].replace('course','');
        key[i] = key[i].substring(0, key[i].length - 1);
        key[i+1] = key[i+1].replace('teacher','');
        key[i+1] = key[i+1].substring(0, key[i+1].length - 1);
        str = str + userId + '-' + key[i] + '-' + value[i] + '-' + key[i+1] + '-' +value[i+1] +',' ;
      }
      str = str.substring(0, str.length - 1);  
      /*--------------------------------------------------------------------------------------------------*/

      wx.request({
         
        url:this.data.addValueUrl, 
        method: "get",
        data: {
          list: str
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: '课程评价提交成功！',
              success: function(res) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }else if(res.data.code == 1){
            wx.showModal({
              title: '提示',
              content: '您已经评价过该课程，请点击确定返回上一页！',
              success: function(res) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        },
        fail: function (res) {
          console.log('发数据失败');
        }
      });
    }
})